#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

set -x

# Since we only use python to train a model for evaluation test, it is sufficient to train the model only with one python version.
# Now it is trained with Python 3.5. If it is not supported anymore, need to use another python version.
# TODO: have a better way to configure how run test only on a single python version
PythonVer=$(python -c "import sys; sys.stdout.write(str(sys.version_info[0])+str(sys.version_info[1])); sys.stdout.flush()")
if [ "$PythonVer" != "35" ]; then
  echo Skip this test for Python 2.7/3.6. Run this test only on Python 3.5 to save test time.
  # Simulate test completes.
  set +x
  echo Evaluation completes
  exit 0
fi

# This test uses pre-trained resnet_20_cifar_10 model in $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY for tests on CPU device
if [[ "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" == "" || ! -d "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" ]]; then
  echo 'This test uses external data that is not part of the CNTK repository. Environment variable CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY must be set to point to the external test data location'
  exit 1
fi

if [ "$OS" == "Windows_NT" ]; then
  TestDataSourceDir=`cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY`
  RunTestDataDir=$TEST_BIN_DIR/data
else
  TestDataSourceDir=$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY
  # On Linux, all dotnet dlls are built into lib dir, so keeping the data dir in the same location
  RunTestDataDir=$TEST_BIN_DIR/../lib/data
fi
mkdir $RunTestDataDir
# Set CUDA_VISIBLE_DEVICES to exclude all gpu if running on cpu device
[ "$TEST_DEVICE" == "cpu" ] && export CUDA_VISIBLE_DEVICES=-1

if [ "$TEST_DEVICE" == "cpu" ]; then
  echo Use pre-trained resnet_20_cifar_10 model and atis.dnn in $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY for tests on CPU device.
  # Unzip data only, without training model
  python train_models_for_evaluation.py -u -d $TEST_DEVICE || exit $?
  cp $TestDataSourceDir/PreTrainedModels/EvalModels/resnet20_cifar10_python.dnn $RunTestDataDir/resnet20.dnn || exit $?
  cp $TestDataSourceDir/PreTrainedModels/EvalModels/atis.dnn $RunTestDataDir/atis.dnn || exit $?
else
  echo Use the on-the-fly trained resnet_20_cifar_10 model for tests on GPU device.
  # Train resnet cifar model and atis model
  python train_models_for_evaluation.py -d $TEST_DEVICE || exit $?
  mv resnet20_0.dnn $RunTestDataDir/resnet20.dnn || exit $?
  mv atis_0.dnn $RunTestDataDir/atis.dnn || exit $?
fi
mv data/train/* $RunTestDataDir || exit $?
cp $TEST_ROOT_DIR/../../Examples/LanguageUnderstanding/ATIS/BrainScript/query.wl $RunTestDataDir || exit $?
cp $TEST_ROOT_DIR/../../Examples/LanguageUnderstanding/ATIS/BrainScript/slots.wl $RunTestDataDir || exit $?

if [ "$OS" == "Windows_NT" ]; then
  pushd $TEST_BIN_DIR > /dev/null
  echo 'Running command "dotnet CNTKLibraryCSEvalExamplesTest.dll" `cygpath -aw $RunTestDataDir`'
  dotnet CNTKLibraryCSEvalExamplesTest.dll `cygpath -aw $RunTestDataDir`
  popd > /dev/null
else
  pushd $TEST_BIN_DIR/../lib > /dev/null
  echo 'Running command "dotnet CNTKLibraryCSEvalExamplesTest.dll" $RunTestDataDir'
  dotnet CNTKLibraryCSEvalExamplesTest.dll $RunTestDataDir
  popd > /dev/null
fi

# This is created by train_models_for_evaluation.py, remove after test
rm -rf ./data
# Remove temp test data dir
rm -rf $RunTestDataDir

ExitCode=$?

exit $ExitCode
